package com.ymatou.iapi.prod.testcase;

import java.util.ArrayList;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.prod.parameter.GetShowSnapshotListBean;
import com.ymatou.iapi.prod.parameter.ProductVersionListModel;
import com.ymatou.iapi.prod.service.GetShowSnapshotListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.BuyerAppProdService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.mongo.ProductDetailModelWapper;
import com.ymttest.utils.EnvSetup;

public class Ts_GetShowSnapshotList {
	private static GetShowSnapshotListBean getShowSnapshotListBean;
	private static GetShowSnapshotListCall getShowSnapshotListCall;
	private static BuyerAppProdService buyerAppProdService = new BuyerAppProdService();
	
	private static int sellerId = Integer.parseInt(EnvSetup.getData("zhangyi_seller002_id"));
	private static String sellerName = EnvSetup.getData("zhangyi_seller002_name");
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量获取商品是否要显示商品快照");
	}

	@Before
	public void caseUp() throws Exception {
		getShowSnapshotListBean = new GetShowSnapshotListBean();
		getShowSnapshotListCall = new GetShowSnapshotListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getShowSnapshotListCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_GetProductInfo_001() {
		 Logger.start(true,"验证商品需要跳转商品快照");
		 try {
			 String productId = buyerAppProdService.modifyProduct(sellerName, sellerId);
			 Thread.sleep(5000);

			 String version = "1.001";
			 ProductDetailModelWapper productDetailModelWapper = new ProductDetailModelWapper();
			 Map<String, Object> productMap = productDetailModelWapper.getProductId(productId);
			 System.out.println(productMap.toString());
			 
			 String latestVersion = productMap.get("Version").toString();
			 
			 ProductVersionListModel productVersionListModel = new ProductVersionListModel();
			 productVersionListModel.setProductId(productId);
			 productVersionListModel.setVersion(version);
			 
			 ArrayList<ProductVersionListModel> productVersionList = new ArrayList<ProductVersionListModel>();
			 productVersionList.add(productVersionListModel);
			 
			 getShowSnapshotListBean.setProductVersionList(productVersionList);
			 
			 getShowSnapshotListCall.setData(getShowSnapshotListBean);
			 getShowSnapshotListCall.callService();
			 int length = 2;
			 
			 if (this.getShowSnapshot(version, latestVersion))
				 length = 0;
			 else 
				 length = 1;
			 
			 Logger.verifyEquals(length, getShowSnapshotListCall.getList().length(), "验证返回值的列表大小是否正确");
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_GetProductInfo_002() {
		 Logger.start(true,"验证商品不需要跳转商品快照");
		 try {
			 String productId = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			 Thread.sleep(6000);
			 //String productId = "ff778a92-c983-4d45-8e4a-a231dfcda196";
			 String version = "1.001";
			 ProductDetailModelWapper productDetailModelWapper = new ProductDetailModelWapper();
			 Map<String, Object> productMap = productDetailModelWapper.getProductId(productId);
			 
			 String latestVersion = productMap.get("Version").toString();
			 ProductVersionListModel productVersionListModel = new ProductVersionListModel();
			 productVersionListModel.setProductId(productId);
			 productVersionListModel.setVersion(version);
			 
			 ArrayList<ProductVersionListModel> productVersionList = new ArrayList<ProductVersionListModel>();
			 productVersionList.add(productVersionListModel);
			 
			 getShowSnapshotListBean.setProductVersionList(productVersionList);
			 
			 getShowSnapshotListCall.setData(getShowSnapshotListBean);
			 getShowSnapshotListCall.callService();
			 int length = 2;
			 
			 if (this.getShowSnapshot(version, latestVersion))
				 length = 0;
			 else 
				 length = 1;
			 
			 Logger.verifyEquals(length, getShowSnapshotListCall.getList().length(), "验证返回值的列表大小是否正确");
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_GetProductInfo_003() {
		 Logger.start(true,"验证批量商品是否跳转商品快照");
		 try {
			 String productId = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");
			 String modifyProductId = buyerAppProdService.modifyProduct(sellerName, sellerId);
			 Thread.sleep(1000);
			 String version = "1.001";
			 
			 ProductDetailModelWapper productDetailModelWapper = new ProductDetailModelWapper();

			 ProductVersionListModel productVersionListModel = new ProductVersionListModel();
			 productVersionListModel.setProductId(productId);
			 productVersionListModel.setVersion(version);
			 
			 ProductVersionListModel modifyProductVersionListModel = new ProductVersionListModel();
			 modifyProductVersionListModel.setProductId(modifyProductId);
			 modifyProductVersionListModel.setVersion(version);
			 
			 ArrayList<ProductVersionListModel> productVersionList = new ArrayList<ProductVersionListModel>();
			 productVersionList.add(productVersionListModel);
			 productVersionList.add(modifyProductVersionListModel);
			 
			 getShowSnapshotListBean.setProductVersionList(productVersionList);
			 
			 getShowSnapshotListCall.setData(getShowSnapshotListBean);
			 getShowSnapshotListCall.callService(); 
			 int length = 0;
			 ArrayList<String> productIdList = new ArrayList<String>();
			 
			 for (ProductVersionListModel model : productVersionList) {
				 Map<String, Object> productMap = productDetailModelWapper.getProductId(model.getProductId());
				 String latestVersion = productMap.get("Version").toString();
				 
				 if (!this.getShowSnapshot(model.getVersion(), latestVersion)) {
					 length++;
					 productIdList.add(model.getProductId());
				 }
			 }
			 
			 Logger.verifyEquals(length, getShowSnapshotListCall.getList().length(), "验证返回值的列表大小是否正确");
			 
			 for (int i = 0; i < productIdList.size(); i++) {
				 Logger.verifyEquals(true, this.productIsInList(productIdList.get(i), getShowSnapshotListCall.getList()), "验证商品id: "
				 		+ productIdList.get(i) + "是否在返回值中存在");
			 }
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	/**
	 * 判断商品是否在商品列表中存在
	 * 
	 * @param productId(需要寻找的商品)，array(列表)
	 * @return Boolean
	 * @throws JSONException 
	 * @throws Exception
	 */
	public boolean productIsInList(String productId, JSONArray array) throws JSONException {
		
		if (array.length() == 0)
			return false;
		
		for (int i = 0; i < array.length(); i++) {
			JSONObject object = array.getJSONObject(i);
			if (object.getString("productId").equalsIgnoreCase(productId))
				return true;
		}
		
		return false;
	}
	
	/**
	 * 判断商品是否需要跳转快照
	 * 
	 * @param version(传入的商品版本)，productVersion(最新的商品版本)
	 * @return Boolean
	 * @throws JSONException 
	 * @throws Exception
	 */
	public Boolean getShowSnapshot(String version, String productVersion) throws Exception {
		if (version.equalsIgnoreCase("0"))
			return false;
		else {
			int bigVersion = Integer.valueOf(version.split("\\.")[0]);
			int productBigVersion = Integer.valueOf(productVersion.split("\\.")[0]) / 1000;
			
			if (bigVersion == productBigVersion)
				return true;
			else
				return false;
		}
	}
}
